/*
 * @Description:
 * @Author: zheng yiliang
 * @Date: 2021-04-08 11:24:14
 * @LastEditTime: 2021-05-06 22:52:08
 * @LastEditors: zheng yiliang
 */
function mapMenu (array) {
  return array.map(item => {
    return {
      id: item._id,
      authName: item.meta.title,
      children: item.children,
      menuId: item.menu_id,
      perms: item.perms,
      icon: item.meta.icon,
      createTime: item.create_time,
      component: item.component,
      parentId: item.parent_id,
      menuType: item.menu_type,
      path: item.path
    }
  })
}

exports.mapMenus = function (menus) {
  const leve0 = menus.filter(menu => menu.parent_id === 0)
  leve0.forEach(L0 => {
    let obj = menus.filter(L1 => L1.parent_id === L0.menu_id)
    obj.forEach(ele => {
      let leve2 = menus.filter(L2 => ele.menu_id === L2.parent_id)
      ele.children = mapMenu(leve2)
    })
    L0.children = mapMenu(obj)
  })
  return mapMenu(leve0)
}

exports.convertImgToBase64 = function (imageFile, callback, errorCallback) {
  try {
    let reader = new FileReader()
    reader.readAsDataURL(imageFile)
    reader.onload = function (e) {
      if (callback) {
        let base64Str = e.target.result
        callback(base64Str)
      }
    }
  } catch (error) {
    console.error(error)
    if (errorCallback) {
      errorCallback(error)
    }
  }
}